from functools import reduce

from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import Ads, Category, Goods, HotGoods
from goods.serializer import Ads_Serializer, Goods_Serializer, HotGoods_Serializer, Category_Serializer

# 轮播图
class CarouselsView(APIView):
    def get(self,request): #获取数据
        cars = Ads.objects.all()
        ser = Ads_Serializer(cars, many=True)
        return Response(ser.data)
# 类别展示
class Cates(APIView):
    def get(self,request): #获取数据
        cates = Category.objects.all()
        ser = Category_Serializer(cates, many=True)
        return Response(ser.data)
# 新品上线
class GoodsView(APIView):
    def get(self, request):
        goods = Goods.objects.all()
        ser = Goods_Serializer(goods, many=True)
        return Response(ser.data)

# 热门商品 HotGoods
class HotGoodsView(APIView):
    def get(self, request):
        goods = HotGoods.objects.all()
        ser = HotGoods_Serializer(goods, many=True)
        return Response(ser.data)


# 最新推荐
class NewGoodsView(APIView):
    def get(self, request):
        # goodid = request.data.get('goodsId')
        goodid = request.query.params('goodsId')
        goods = [Goods.objects.get(id=i) for i in goodid]
        goo = reduce(lambda x, y: x | y, '-sellingPrice')
        # goods = HotGoods.objects.all()
        ser = Goods_Serializer(goo, many=True)
        return Response(ser.data)
